package com.qiye.livevideo.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.live.model.v20161101.*;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.qiye.livevideo.model.AliCloudLiveInfo;
import com.qiye.livevideo.service.AliCloudLiveInfoService;

import javax.annotation.Resource;

/**
 * Created on 2019/5/2.
 *
 * @author meishijun
 */
public class AliLiveSdkUtils {
    /**
     * 初始化客户端
     * @return
     */
    public static IAcsClient getIAcsClient(String accessKeyId,String accessKeySeret){
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai",accessKeyId , accessKeySeret);
        try {
            //添加自定义endpoint
            DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "live", "live.aliyuncs.com");
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new DefaultAcsClient(profile);
    }



    /**
     * 根据查询条件获取正在推的流
     * @param domainName
     * @param appName
     * @param streamName
     * @param startTime
     * @param endTime
     * @return
     */
    public static  DescribeLiveStreamsPublishListResponse  getFlowList(String domainName,String appName,String streamName,String startTime,String endTime,String accessKeyId,String accessKeySeret ){
        DescribeLiveStreamsPublishListRequest describeLiveStreamsPublishListRequest = new DescribeLiveStreamsPublishListRequest();
        describeLiveStreamsPublishListRequest.setDomainName(domainName);
        describeLiveStreamsPublishListRequest.setStartTime(startTime);
        describeLiveStreamsPublishListRequest.setEndTime(endTime);
        if(appName!=null && !appName.isEmpty()){
            describeLiveStreamsPublishListRequest.setAppName(appName);
        }
        if(streamName!=null && !streamName.isEmpty()){
            describeLiveStreamsPublishListRequest.setStreamName(streamName);
        }
        try {
            IAcsClient client = getIAcsClient(accessKeyId,accessKeySeret);
            DescribeLiveStreamsPublishListResponse httpResponse = client.getAcsResponse(describeLiveStreamsPublishListRequest);
            return httpResponse;
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取当前正在推的流信息------每分钟最多调取1000次， 超过次数会导致应用不可用
     * @param domainName
     * @param appName
     * @param streamName
     * @return
     */
    public static DescribeLiveStreamsOnlineListResponse getNowFlowList(String domainName, String appName, String streamName,String accessKeyId,String accessKeySeret){
        DescribeLiveStreamsOnlineListRequest describeLiveStreamsPublishListRequest = new DescribeLiveStreamsOnlineListRequest();
        describeLiveStreamsPublishListRequest.setDomainName(domainName);
        if(appName!=null && !appName.isEmpty()){
            describeLiveStreamsPublishListRequest.setAppName(appName);
        }
        if(streamName!=null && !streamName.isEmpty()){
            describeLiveStreamsPublishListRequest.setStreamName(streamName);
        }
        try {
            IAcsClient client = getIAcsClient(accessKeyId,accessKeySeret);
            DescribeLiveStreamsOnlineListResponse httpResponse = client.getAcsResponse(describeLiveStreamsPublishListRequest);
            return httpResponse;
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取域名下的拉流配置信息列表
     * @param domainName
     * @return
     */
    public static DescribeLivePullStreamConfigResponse getLiveNowFlowList(String domainName,String accessKeyId,String accessKeySeret){
        DescribeLivePullStreamConfigRequest describeLivePullStreamConfigRequest = new DescribeLivePullStreamConfigRequest();
        describeLivePullStreamConfigRequest.setDomainName(domainName);
        try {
            IAcsClient client = getIAcsClient(accessKeyId,accessKeySeret);
            DescribeLivePullStreamConfigResponse httpResponse = client.getAcsResponse(describeLivePullStreamConfigRequest);
            return httpResponse;
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }


}
